package com.oocl.listener;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.oocl.dto.MerchantDTO;
import com.oocl.exception.APlatformOfflineException;
import com.oocl.factory.ServiceFactory;
import com.oocl.pojo.Merchant;
import com.oocl.service.MerchantService;
import com.oocl.util.ObjConvertUtil;
import com.oocl.util.URLConnectionUtil;
import com.oocl.vo.MerchantVO;

/**
 * 系统启动的时候，加载所有的 白 状态 商家信息到 前台页面
 * @author XIEMI2
 *
 */
public class LoadAllMerchantsListener implements ServletContextListener {

	private MerchantService merchantService;
    /**
     * Default constructor. 
     */
    public LoadAllMerchantsListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent sce) {
    	
		try {
			merchantService = ServiceFactory.getMerchantService();
			
	    	// 从 A 平台 拿到所有白状态的商家信息
			List<MerchantDTO> merchantDTOs;
			merchantDTOs = URLConnectionUtil.getAllPassMerchant();
			// 完整商家 的头像 信息
			List<Merchant> merchants = merchantService.getMerchants(merchantDTOs);
			// 封装成 vo
			List<MerchantVO> merchantVOs = ObjConvertUtil.merchantDOsToVOs(merchants);
			
			//测试
			for(MerchantVO vo:merchantVOs){
				System.out.println(vo.toString());
			}
			
			sce.getServletContext().setAttribute("AllMerchantsInfo", merchantVOs);
			
		} catch (APlatformOfflineException e) {
			System.out.println("A 平台 down 机了");
			e.printStackTrace();
		}
		
		
    	
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
    }
	
}
